package com.common.util;

import cn.hutool.system.oshi.OshiUtil;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 大忽悠
 * @create 2022/10/9 19:32
 */
public class StringUtil {
    public static final String DELIMITER =",";

    public static List<Integer> strIdToListInt(String strId){
        return Arrays.stream(strId.split(DELIMITER)).map(Integer::valueOf).collect(Collectors.toList());
    }

    public static boolean isNullOrEmpty(String value) {
        if(value==null){
            return true;
        }
        return value.trim().isEmpty() || "".equals(value) || "\"\"".equals(value.trim());
    }

    /**
     * 清除目标字符串结尾的"/"字符
     */
    public static String cleanEndFileSeparator(String targetStr){
        return (targetStr.endsWith("/")||targetStr.endsWith("\\"))?targetStr.substring(0,targetStr.length()-1):targetStr;
    }

    public static String cleanStartFileSeparator(String targetStr) {
        return (targetStr.startsWith("/")||targetStr.startsWith("\\"))?targetStr.substring(1,targetStr.length()):targetStr;
    }

    public static String cleanStartAndEndFileSeparator(String targetStr){
        return cleanStartFileSeparator(cleanEndFileSeparator(targetStr));
    }

    public static String stripStrFromBegin(String stripStr,String targetStr){
        if(!targetStr.startsWith(stripStr)){
            return targetStr;
        }
        return targetStr.substring(stripStr.length());
    }

    public static String pathMerge(Boolean ignoreCleanFirstPathStartSeparator,String ... paths){
        StringBuilder stringBuilder=new StringBuilder();
        Integer index=1;
        for (String path : paths) {
            //忽略为空的路径
            if(StringUtil.isNullOrEmpty(path)){
                continue;
            }
            if(ignoreCleanFirstPathStartSeparator&&index.equals(1)){
                stringBuilder.append(cleanEndFileSeparator(path)).append(File.separator);
                index--;
                continue;
            }
            stringBuilder.append(cleanStartAndEndFileSeparator(path)).append(File.separator);
        }
        String path = cleanEndFileSeparator(stringBuilder.toString());
        return SystemUtil.isWindows()?path.replace('/','\\'):path.replace('\\','/');
    }

    public static String urlMerge(String ... paths){
        StringBuilder stringBuilder=new StringBuilder();
        for (String path : paths) {
            //忽略为空的路径
            if(StringUtil.isNullOrEmpty(path)){
                continue;
            }
            stringBuilder.append(cleanStartAndEndFileSeparator(path)).append("/");
        }
        return cleanEndFileSeparator(stringBuilder.toString()).replace('\\','/');
    }
}
